﻿using Apewer;
using Apewer.Internals;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Apewer.Source
{

    /// <summary>数据库记录通用字段模型。</summary>
    /// <remarks>带有 Independent 特性的模型不包含此类型声明的属性。</remarks>
    [Serializable]
    public abstract class Record : IRecord
    {

        /// <summary>记录主键，一般使用 GUID 的字符串形式。</summary>
        /// <remarks>
        /// <para>注：</para>
        /// <para>1. 默认长度为 32，需要修改长度时应该重写此属性；</para>
        /// <para>2. 带有 Independent 特性的模型不包含此属性。</para>
        /// </remarks>
        [Column("_key", ColumnType.NVarChar, 32)]
        public virtual string Key { get; set; }

        /// <summary>记录的标记，Int64 类型，区分记录的状态。</summary>
        /// <remarks>带有 Independent 特性的模型不包含此属性。</remarks>
        [Column("_flag", ColumnType.Integer)]
        public virtual long Flag { get; set; }

        /// <summary>重置 Key 属性的值。</summary>
        public virtual void ResetKey() => Key = TextUtility.Key();

        /// <summary></summary>
        public Record()
        {
            ResetKey();
            Flag = DefaultFlag;
        }

        #region static

        /// <summary>创建 Record 对象时的默认 Flag 值。</summary>
        public static long DefaultFlag { get; set; }

        #endregion

        #region 运算符。

        /// <summary>从 Record 到 Boolean 的隐式转换，判断 Record 对象不为 NULL。</summary>
        public static implicit operator bool(Record instance) => instance != null;

        #endregion
    }

}
